﻿using System;

namespace NET.HLW5a.Snippets.Patterns
{
    public class Singleton
    {

        public class StaticSingleton
        {
            private static readonly StaticSingleton Singleton = new StaticSingleton();
            public static StaticSingleton Instance
            {
                get { return Singleton; }
                private set { }
            }

            private StaticSingleton()
            {
            }
        }

        /*public class LazyThreadSafeSingleton
        {
            private static readonly Lazy<LazyThreadSafeSingleton> Singleton = new Lazy<LazyThreadSafeSingleton>(() => new LazyThreadSafeSingleton());
            public static LazyThreadSafeSingleton Instance
            {
                get { return Singleton.Value; }
                private set { }
            }

            private LazyThreadSafeSingleton()
            {
            }
        }*/

        public class NonThreadSafeSingleton
        {
            private static NonThreadSafeSingleton Singleton;
            public static NonThreadSafeSingleton Instance
            {
                get { return Singleton ?? (Singleton = new NonThreadSafeSingleton()); }
                private set { }
            }

            private NonThreadSafeSingleton()
            {
            }
        }

        public class ThreadSafeSingleton
        {
            private static volatile ThreadSafeSingleton Singleton;
            private static Object Sync = new Object();
            public static ThreadSafeSingleton Instance
            {
                get { if (Singleton == null) lock (Sync) if (Singleton == null) Singleton = new ThreadSafeSingleton(); return Singleton; }
                private set { }
            }

            private ThreadSafeSingleton()
            {
            }
        }
    }
}
